package com.zsy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.dao.CategoryDao;
import com.zsy.dao.SetmealDao;
import com.zsy.dao.SetmealDishDao;
import com.zsy.dto.SetmealDto;
import com.zsy.exception.CustomException;
import com.zsy.pojo.*;
import com.zsy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired(required = false)
    private SetmealDishDao setmealDishDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    @Transactional
    public void save(SetmealDto setmealDto) {
        //补全创建更新时间信息
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //调用save方法
        setmealDao.save(setmealDto);
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();

        List<SetmealDish> collect = dishes.stream().map(dish -> {
            dish.setSetmealId(setmealDto.getId());
            dish.setCreateTime(LocalDateTime.now());
            dish.setCreateUser(setmealDto.getCreateUser());
            dish.setUpdateTime(LocalDateTime.now());
            dish.setUpdateUser(setmealDto.getUpdateUser());
            dish.setSort(0);
            return dish;
        }).collect(Collectors.toList());

        setmealDishDao.save(collect);
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {

        PageHelper.startPage(page,pageSize);

        List<Setmeal> setmealList = setmealDao.findByName(name);

        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmealList);

        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();

        for (Setmeal setmeal : setmealPageInfo.getList()) {
            //new 出Dto
            SetmealDto setmealDto = new SetmealDto();
            //将查询出来的Setmeal复制进setmealDto
            BeanUtils.copyProperties(setmeal,setmealDto);
            //获得套餐分类
            Category byId = categoryDao.findById(setmeal.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            //加入集合
            setmealDtos.add(setmealDto);
        }

        return new Page<>(setmealDtos, setmealPageInfo.getTotal(), page, pageSize);
    }

    @Override
    public SetmealDto findById(Long id) {
        Setmeal setmeal = setmealDao.idFind(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        List<SetmealDish> setmealDishes =  setmealDishDao.findById(setmeal.getId());

        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    @Override
    @Transactional
    public void update(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());

        setmealDao.update(setmealDto);

        setmealDishDao.delete(setmealDto.getId());

        List<SetmealDish> collect = setmealDto.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishDao.save(collect);


    }

    @Override
    public void delete(List<Long> ids) {
        long id = setmealDao.findByIds(ids);
        if(id>0)
           throw new CustomException("存在套餐是在售状态，无法删除");

        setmealDao.delete(ids);
    }

    @Override
    public List<Setmeal> list(Long categoryId, Integer status) {


        return setmealDao.list(categoryId,status);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        setmealDao.updateStatus(status,ids);
    }

}
